import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/user/users'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Roles'
import Goods from '../components/commodity/Goods'
import Params from '../components/commodity/Params'
import Categories from '../components/commodity/Categories'
import Orders from '../components/order/orders'
import Reports from '../components/statistics/Reports'
import Add from '../components/commodity/Add'

Vue.use(VueRouter)
const router = new VueRouter({
  routes:[
	  {path:'/',redirect: '/login' },
	  {path:'/login',component: Login },
	  {path:'/home',
	  component: Home,
	  redirect:'/users',			
	  children:[
		  {path:'/welcome',component:Welcome},
		  {path:'/users',component:Users},
		  {path:'/rights',component:Rights},
		  {path:'/roles',component:Roles},
		  {path:'/goods',component:Goods},
		  {path:'/params',component:Params},
		  {path:'/categories',component:Categories},
		  {path:'/orders',component:Orders},
		  {path:'/reports',component:Reports},
		  {path:'/goods/add',component:Add},
			]		//添加子路由
	  }
  ]
})

//挂载路由导航卫士
router.beforeEach((to,from,next) =>{
		//to 将要访问的路径
		//from 代表从那个路径跳转过来的
		//next 是一个函数 表示放行
		if(to.path === '/login') return next()		//判断 是否为 /login 如果是就放行
		const tokenStr = window.sessionStorage.getItem('token')	
		if(!tokenStr) return next('/login')		//判断是否还有 token
		next()
})

export default router
